package Server;

/**
 * messages from the client passed here will be parsed so that the server can use them
 * @author David
 *
 */

public class StringParser {

	/**
	 * First part of every message
	 */
	String function;
	/**
	 * for user stuff 
	 */
	String name, password;
	
	protected StringParser(String message){
		message = parseFunction(message);
		
		if (function.equalsIgnoreCase("login")){
			message = parseName(message);
			message = parsePassword(message);
		}
		
	}
	
	public String parseFunction(String message) {
		
		int i = 0;
		int j = 0;
		//put into buffer the characters of the function
		char[] buffer = null;
		char[] temp = message.toCharArray();
		while(temp[i] != '!' && temp[i] != ','){
			buffer[i] = temp[i];
			i++;
		}
		i++;
		////////////////////////////////
		
		//create updated message
		char[] newMessage = null;
		while(temp[i] != '!'){
			newMessage[j] = temp[i];
			i++;
			j++;
		}
		newMessage[j] = temp[i];
		String str = newMessage.toString();
		////////////////////////
		
		function = buffer.toString();
		return str;
	}
	
	private String parseName(String message){
		int i = 0;
		int j = 0;
		//put into buffer the characters of the function
		char[] buffer = null;
		char[] temp = message.toCharArray();
		while(temp[i] != '!' && temp[i] != ','){
			buffer[i] = temp[i];
			i++;
		}
		i++;
		////////////////////////////////
		
		//create updated message
		char[] newMessage = null;
		while(temp[i] != '!'){
			newMessage[j] = temp[i];
			i++;
			j++;
		}
		newMessage[j] = temp[i];
		String str = newMessage.toString();
		////////////////////////
		
		name = buffer.toString();
		return str;
	}
	
	private String parsePassword(String message){
		int i = 0;
		int j = 0;
		//put into buffer the characters of the function
		char[] buffer = null;
		char[] temp = message.toCharArray();
		while(temp[i] != '!' && temp[i] != ','){
			buffer[i] = temp[i];
			i++;
		}
		i++;
		////////////////////////////////
		
		//create updated message
		char[] newMessage = null;
		while(temp[i] != '!'){
			newMessage[j] = temp[i];
			i++;
			j++;
		}
		newMessage[j] = temp[i];
		String str = newMessage.toString();
		////////////////////////
		
		password = buffer.toString();
		return str;
	}
	
	public String getFunction(){
		return function;
	}
	
	public String getName(){
		return name;
	}
	
	public String getPassword(){
		return password;
	}
}
